﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;
using _WebShared.Classes;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.IO;
using System.Net;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_BankAccounts : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MAS_Banks bank = new B_FIM_MAS_Banks();
        IBLL_FIM_MAS_BankBranchs branch = new B_FIM_MAS_BankBranchs();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();
        IBLL_FIM_MAS_BankAccountTypes acctype = new B_FIM_MAS_BankAccountTypes();
        IBLL_FIM_MAS_BankAccountTypeGroups typegrp = new B_FIM_MAS_BankAccountTypeGroups();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        MailMerge mailmerge = new MailMerge();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                //UpdateUI1("ViewAll");
                UpdateUI2("ViewAll");
                LoadRadGridBankAcc(null);
                LoadDDLSavingBankAccSeq("");
                LoadDDLBankAccTypeGrpSeq();
                //PanelSubSource.Visible = false;
                RadGridBankAcc.Enabled = true;
                PanelControl2.Enabled = true;
                LoadBank();
                
               // DropDownList_BankBranch1.LoadRootNodes(null);
            }
        }

         protected void LoadBank()
        {
            ddlBank.DataSource = bank.GetList();
            ddlBank.DataBind();
        }

        protected void LoadBranch()
        {
            if (!string.IsNullOrEmpty(ddlBank.SelectedValue))
            {
                ddlBranch.DataSource = branch.GetList(ddlBank.SelectedValue);
                ddlBranch.DataBind();
            }
            else
            {
                ddlBranch.DataSource = null;
                ddlBranch.DataBind();
            }
        }

        protected void LoadDDLBankAccTypeGrpSeq()
        {
            cboRadGrpName.DataSource = typegrp.GetList();
            cboRadGrpName.DataBind();
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetFull(true);
            ddlBgmSourceSeq.DataBind();
        }

        protected void LoadDDLWorkAreaSeq()
        {
            ddlWorkAreaSeq.DataSource = area.GetList();
            ddlWorkAreaSeq.DataBind();
        }

        protected void LoadDDLSavingBankAccSeq(string strBgmSourceSeq)
        {
            //if (strBgmSourceSeq.Length > 0)
            //{
            //    ddlSavingBankAccSeq.DataSource = acc.SelectDropDownList("1"/*ออมทรัพย์*/, strBgmSourceSeq);
            //}
            //else
            //{
            //    ddlSavingBankAccSeq.DataSource = null;
            //}
            ddlSavingBankAccSeq.DataSource = acc.GetList("1"/*ออมทรัพย์*/, strBgmSourceSeq, ddlBank.SelectedValue, null);
            ddlSavingBankAccSeq.DataBind();
        }

        //protected void LoadDDLSubSourceSeq(string strBgmSourceSeq)
        //{
        //    if (strBgmSourceSeq.Length > 0)
        //    {
        //        ddlSubSourceSeq.DataSource = sub.GetList(strBgmSourceSeq);
        //    }
        //    else
        //    {
        //        ddlSubSourceSeq.DataSource = null;
        //    }

        //    ddlSavingBankAccSeq.DataSource = acc.SelectDropDownList("1"/*ออมทรัพย์*/, strBgmSourceSeq);
        //    ddlSubSourceSeq.DataBind();
        //}

        protected void LoadRadGridBankAcc(string strBankAccTypeGrpSeq)
        {
            RadGridBankAcc.DataSource = acc.GetFull(strBankAccTypeGrpSeq);
            RadGridBankAcc.DataBind();
        }

        //protected void UpdateUI1(string strCommand)
        //{
        //    switch (strCommand)
        //    {
        //        case "ViewAll":
        //        case "Insert":
        //        case "Update":
        //            cmdViewAll1.Visible = false;
        //            cmdAddNew1.Visible = true;
        //            cmdEdit1.Visible = true;
        //            cmdDelete1.Visible = true;
        //            cmdInsert1.Visible = false;
        //            cmdUpdate1.Visible = false;

        //            PanelRadGridBankAccTypes.Visible = true;
        //            PanelEditBankAccTypes.Visible = false;
        //            break;
        //        case "AddNew":
        //            cmdViewAll1.Visible = true;
        //            cmdAddNew1.Visible = false;
        //            cmdEdit1.Visible = false;
        //            cmdDelete1.Visible = false;
        //            cmdInsert1.Visible = true;
        //            cmdUpdate1.Visible = false;

        //            PanelRadGridBankAccTypes.Visible = false;
        //            PanelEditBankAccTypes.Visible = true;
        //            break;
        //        case "Edit":
        //            cmdViewAll1.Visible = true;
        //            cmdAddNew1.Visible = false;
        //            cmdEdit1.Visible = false;
        //            cmdDelete1.Visible = false;
        //            cmdInsert1.Visible = false;
        //            cmdUpdate1.Visible = true;

        //            PanelRadGridBankAccTypes.Visible = false;
        //            PanelEditBankAccTypes.Visible = true;
        //            break;
        //    }
        //}

        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll2.Visible = false;
                    cmdAddNew2.Visible = true;
                    cmdEdit2.Visible = true;
                    cmdDelete2.Visible = true;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = false;

                    PanelRadGridBankAcc.Visible = true;
                    PanelEditBankAcc.Visible = false;
                    //PanelControl1.Enabled = true;
                    //PanelRadGridBankAccTypes.Enabled = true;
                    break;
                case "AddNew":
                    cmdViewAll2.Visible = true;
                    cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = true;
                    cmdUpdate2.Visible = false;

                    PanelRadGridBankAcc.Visible = false;
                    PanelEditBankAcc.Visible = true;
                    //PanelControl1.Enabled = false;
                    //PanelRadGridBankAccTypes.Enabled = false;
                    break;
                case "Edit":
                    cmdViewAll2.Visible = true;
                    cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = true;

                    PanelRadGridBankAcc.Visible = false;
                    PanelEditBankAcc.Visible = true;
                    //PanelControl1.Enabled = false;
                    //PanelRadGridBankAccTypes.Enabled = false;
                    break;
            }
        }

        //protected void ClearControl1()
        //{
        //    lblBankAccTypeSeq.Text = "";
        //    ddlBankAccTypeGrpSeq.SelectedIndex = -1;
        //    txtBankAccTypeName.Text = "";
        //}

        protected void ClearControl2()
        {
            //lblBankAccTypeSeq2.Text = "";
            //lblBankAccTypeName2.Text = "";
            lblBankAccSeq.Text = "";
            txtBankAccNo.Text = "";
            txtBankAccName.Text = "";
            txtBankAccNameEng.Text = "";
            ddlBgmSourceSeq.SelectedIndex = -1;
            ddlWorkAreaSeq.SelectedIndex = -1;
            ddlSavingBankAccSeq.SelectedIndex = -1;
            cboRadGrpName.SelectedIndex = -1;
            txtRemarks.Text = "";
            chkIsAdvance.Checked = false;
        }

        //============================ control events ==============================

        protected void RadGridBankAccTypes_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            {
                e.Item.Selected = true;
                string strBankAccTypeGrpSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["bank_acc_type_grp_seq"].Text;
                RadGridBankAcc.Enabled = true;
                PanelControl2.Enabled = true;
                LoadRadGridBankAcc(strBankAccTypeGrpSeq);
            }
            else if (e.CommandName == "download_file")
            {
                GridDataItem item = (GridDataItem)e.Item;

                string strPath = ("../FileUpload/FileBankAcc");
                string strFileName = item["path"].Text;
                string strFileExtension = new FileInfo(Server.MapPath(strPath)).Extension;
                WebClient req = new WebClient();
                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.ClearContent();
                response.ClearHeaders();
                response.Buffer = true;
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strPath + strFileExtension) + "\"");
                byte[] data = req.DownloadData(MapPath(strPath));
                response.BinaryWrite(data);
                response.End();
            }
        }

        protected void RadGridBankAcc_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                LoadRadGridBankAcc(null);
            }
            //else if (e.CommandName == "History")
            //{
            //    RadWindow1.Visible = true;
            //    RadWindow1.Controls.Add(new  FIM_Mas_BankAccountHistoriess());
            //}
        }

        //protected void RadGridBankAccTypes_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        //{
        //    if (!e.IsFromDetailTable)
        //    {
        //        LoadRadGridBankAccTypes();
        //    }
        //}

        //protected void cmdViewAll1_Click(object sender, EventArgs e)
        //{
        //    UpdateUI1("ViewAll");
        //    ClearControl1();
        //}

        //protected void cmdAddNew1_Click(object sender, EventArgs e)
        //{
        //    UpdateUI1("AddNew");
        //}

        //protected void cmdEdit1_Click(object sender, EventArgs e)
        //{
        //    if (RadGridBankAccTypes.SelectedItems.Count > 0)
        //    {
        //        UpdateUI1("Edit");

        //        string strBankAccTypeSeq = RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_seq"].Text;
        //        string strBankAccTypeGrpSeq = RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_grp_seq"].Text;
        //        string strBankAccTypeName = RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_name"].Text;

        //        lblBankAccTypeSeq.Text = main.WebString(strBankAccTypeSeq);
        //        ddlBankAccTypeGrpSeq.SelectedValue = main.WebString(strBankAccTypeGrpSeq);
        //        txtBankAccTypeName.Text = main.WebString(strBankAccTypeName);
        //    }
        //    else
        //    {
        //        main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
        //    }
        //}

        //protected void cmdDelete1_Click(object sender, EventArgs e)
        //{
        //    if (RadGridBankAccTypes.SelectedItems.Count > 0)
        //    {
        //        M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();
        //        string strBankAccTypeSeq = RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_seq"].Text;
        //        string strUserID = Session["EmpSeq"].ToString();
        //        DateTime dtNow = DateTime.Now;

        //        info.bank_acc_type_seq = Convert.ToInt32(strBankAccTypeSeq);
        //        info.updated_by = main.ConvertNullInt(strUserID);
        //        info.updated_when = main.ConvertNullDateTime(dtNow);

        //        acctype.Delete(info);

        //        LoadRadGridBankAccTypes();
        //        RadGridBankAccTypes.DataBind();
        //    }
        //}

        //protected void cmdInsert1_Click(object sender, EventArgs e)
        //{
        //    M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();
        //    string strBankAccTypeGrpSeq = ddlBankAccTypeGrpSeq.SelectedValue;
        //    string strBankAccTypeName = txtBankAccTypeName.Text;
        //    string strUserID = Session["EmpSeq"].ToString();
        //    DateTime dtNow = DateTime.Now;

        //    info.bank_acc_type_grp_seq = main.ConvertNullInt(strBankAccTypeGrpSeq);
        //    info.bank_acc_type_name = strBankAccTypeName;

        //    info.created_by = main.ConvertNullInt(strUserID);
        //    info.created_when = main.ConvertNullDateTime(dtNow);

        //    acctype.Insert(info);

        //    LoadRadGridBankAccTypes();
        //    RadGridBankAccTypes.DataBind();
        //    UpdateUI1("Insert");
        //    ClearControl1();
        //}

        //protected void cmdUpdate1_Click(object sender, EventArgs e)
        //{
        //    M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();
        //    string strBankAccTypeSeq = lblBankAccTypeSeq.Text;
        //    string strBankAccTypeGrpSeq = ddlBankAccTypeGrpSeq.SelectedValue;
        //    string strBankAccTypeName = txtBankAccTypeName.Text;
        //    string strUserID = Session["EmpSeq"].ToString();
        //    DateTime dtNow = DateTime.Now;

        //    info.bank_acc_type_seq = Convert.ToInt32(strBankAccTypeSeq);
        //    info.bank_acc_type_grp_seq = main.ConvertNullInt(strBankAccTypeGrpSeq);
        //    info.bank_acc_type_name = strBankAccTypeName;

        //    info.updated_by = main.ConvertNullInt(strUserID);
        //    info.updated_when = main.ConvertNullDateTime(dtNow);

        //    acctype.Update(info);

        //    LoadRadGridBankAccTypes();
        //    RadGridBankAccTypes.DataBind();
        //    UpdateUI1("Update");
        //    ClearControl1();
        //}

        protected void cmdViewAll2_Click(object sender, EventArgs e)
        {
            UpdateUI2("ViewAll");

            ClearControl2();
        }

        protected void cmdAddNew2_Click(object sender, EventArgs e)
        {
            UpdateUI2("AddNew");

            //string strBankAccTypeGrpSeq = "";// RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_grp_seq"].Text;
            //string strBankAccTypeGrpName = "";//RadGridBankAccTypes.Items[RadGridBankAccTypes.SelectedItems[0].ItemIndex]["bank_acc_type_grp_name"].Text;

            //lblBankAccTypeSeq2.Text = strBankAccTypeGrpSeq;
            //lblBankAccTypeName2.Text = strBankAccTypeGrpName;
            
            LoadDDLBgmSourceSeq();

            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //if (strBgmSourceSeq.Trim().Length > 0)
            //{
            //    LoadDDLSubSourceSeq(strBgmSourceSeq);
            //}
            //else
            //{
            //    //LoadDDLSavingBankAccSeq("");
            //}

            //PanelSubSource.Visible = ddlSubSourceSeq.Items.Count > 0;

            LoadDDLWorkAreaSeq();

            
        }

        protected void cmdEdit2_Click(object sender, EventArgs e)
        {
            if (RadGridBankAcc.SelectedItems.Count > 0)
            {
                UpdateUI2("Edit");

                LoadDDLBgmSourceSeq();
                LoadDDLWorkAreaSeq();
                GridDataItem item = RadGridBankAcc.Items[RadGridBankAcc.SelectedItems[0].ItemIndex];

                string strBankAccSeq = item["bank_acc_seq"].Text;
                string strBankAccNo = item["bank_acc_no"].Text;
                string strBankAccName = item["bank_acc_name"].Text;
                string strBankAccNameEng = item["bank_acc_name_eng"].Text;
                string strBankAccTypeGrpSeq = item["bank_acc_type_grp_seq"].Text;
                string strBankAccTypeGrpName = item["bank_acc_type_grp_name"].Text;
                string strBranchSeq = item["branch_seq"].Text;
                string strBankID = item["bank_id"].Text;
                string strBankName = item["bank_name"].Text;
                string strBranchID = item["branch_id"].Text;
                string strBranchName = item["branch_name"].Text;
                string strBgmSourceSeq = item["bgm_source_seq"].Text;
                string strBankSeq = item["bank_seq"].Text;
                //string strSubSourceSeq = item["sub_source_seq"].Text;
                string strSavingBankAccSeq = item["saving_bank_acc_seq"].Text;
                string strWorkAreaSeq = item["work_area_seq"].Text;
                string strRemarks = item["remarks"].Text;
                string strFileUploadName = item["path"].Text;

                lblBankAccSeq.Text = main.WebString(strBankAccSeq);
                txtBankAccNo.Text = main.WebString(strBankAccNo);
                txtBankAccName.Text = main.WebString(strBankAccName);
                txtBankAccNameEng.Text = main.WebString(strBankAccNameEng);
                cboRadGrpName.SelectedValue = main.WebString(strBankAccTypeGrpSeq);
                ddlWorkAreaSeq.SelectedValue = main.WebString(strWorkAreaSeq);
                txtRemarks.Text = main.WebString(strRemarks);
                ddlSavingBankAccSeq.SelectedValue = main.WebString(strSavingBankAccSeq);
                ddlBank.SelectedValue = main.WebString(strBankSeq);
                ddlBank_SelectedIndexChanged(null,null);
                ddlBranch.SelectedValue = main.WebString(strBranchSeq);
                cboRadGrpName_SelectedIndexChanged(null,null);
                chkIsAdvance.Checked = item["path"].Text == "1";

                //    , main.WebString(strBankID + " : " + strBankName + " | " + strBranchID + " : " + strBranchName));
                //ddlBgmSourceSeq.SelectedValue = main.WebString(strBgmSourceSeq);

                //if (strBgmSourceSeq.Trim().Length > 0)
                //{
                //    LoadDDLSubSourceSeq(strBgmSourceSeq);
                //}
                //else
                //{
                //    //LoadDDLSavingBankAccSeq("");
                //}

                //PanelSubSource.Visible = ddlSubSourceSeq.Items.Count > 0;

                //ddlSubSourceSeq.SelectedValue = main.WebString(strSubSourceSeq);

                //lblBankAccTypeSeq2.Text = main.WebString(strBankAccTypeGrpSeq);
                //lblBankAccTypeName2.Text = main.WebString(strBankAccTypeGrpName);

            }
            else
            {
            }
        }

        protected void cmdDelete2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_BankAccounts info = new M_FIM_MAS_BankAccounts();
            string strPK = RadGridBankAcc.Items[RadGridBankAcc.SelectedItems[0].ItemIndex]["bank_acc_seq"].Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.bank_acc_seq = Convert.ToInt32(strPK);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            acc.Delete(info);
            LoadRadGridBankAcc(null);
            RadGridBankAcc.DataBind();
        }

        protected void cmdInsert2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_BankAccounts info = new M_FIM_MAS_BankAccounts();
            //string strBankAccSeq = lblBankAccSeq.Text;
            string strBankAccNo = txtBankAccNo.Text;
            string strBankAccName = txtBankAccName.Text;
            string strBankAccNameEng = txtBankAccNameEng.Text;
            string strBankAccTypeGrpSeq = cboRadGrpName.SelectedValue;
            string strBranchSeq = ddlBranch.SelectedValue;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strSubSourceSeq = (PanelSubSource.Visible ? ddlSubSourceSeq.SelectedValue : null);
            string strSavingBankAccSeq = ddlSavingBankAccSeq.SelectedValue;
            string strWorkAreaSeq = ddlWorkAreaSeq.SelectedValue;
            string strRemarks = txtRemarks.Text;
            HttpPostedFile file = Request.Files["FileUpload"];
            string strPath = ("../FileUpload/FileBankAcc");
            string strFileUploadName = DateTime.Now.Ticks.ToString() + (file);
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.bank_acc_no = strBankAccNo;
            info.bank_acc_name = strBankAccName;
            info.bank_acc_name_eng = strBankAccNameEng;
            info.bank_acc_type_grp_seq = main.ConvertNullInt(strBankAccTypeGrpSeq);
            info.branch_seq = Convert.ToInt32(strBranchSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            //info.sub_source_seq = main.ConvertNullInt(strSubSourceSeq);
            info.saving_bank_acc_seq = main.ConvertNullInt(strSavingBankAccSeq);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.remarks = strRemarks;
            info.path = chkIsAdvance.Checked ? "1" : "0";

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            DirectoryInfo DirInfo = new DirectoryInfo(Server.MapPath(strPath));

            acc.Insert(info);

            LoadRadGridBankAcc(null);
            RadGridBankAcc.DataBind();
            UpdateUI2("Insert");
            ClearControl2();
        }

        protected void cmdUpdate2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_BankAccounts info = new M_FIM_MAS_BankAccounts();
            string strBankAccSeq = lblBankAccSeq.Text;
            string strBankAccNo = txtBankAccNo.Text;
            string strBankAccName = txtBankAccName.Text;
            string strBankAccNameEng = txtBankAccNameEng.Text;
            string strBankAccTypeGrpSeq = cboRadGrpName.SelectedValue;
            string strBranchSeq = ddlBranch.SelectedValue;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strSubSourceSeq = (PanelSubSource.Visible ? ddlSubSourceSeq.SelectedValue : null);
            string strSavingBankAccSeq = ddlSavingBankAccSeq.SelectedValue;
            string strWorkAreaSeq = ddlWorkAreaSeq.SelectedValue;
            string strRemarks = txtRemarks.Text;
            string strUserID = Session["EmpSeq"].ToString();

            //-------- Upload File ---------------
            string strPath = ("../FileUpload/FileBankAcc");
            //string serverFileName = "";
            //string strFileUploadName = "";
            //string strFileExtension = "";

            DateTime dtNow = DateTime.Now;

            info.bank_acc_seq = Convert.ToInt32(strBankAccSeq);
            info.bank_acc_no = strBankAccNo;
            info.bank_acc_name = strBankAccName;
            info.bank_acc_name_eng = strBankAccNameEng;
            info.bank_acc_type_grp_seq = main.ConvertNullInt(strBankAccTypeGrpSeq);
            info.branch_seq = Convert.ToInt32(strBranchSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            //info.sub_source_seq = main.ConvertNullInt(strSubSourceSeq);
            info.saving_bank_acc_seq = main.ConvertNullInt(strSavingBankAccSeq);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.remarks = strRemarks;
            info.path = chkIsAdvance.Checked ? "1" : "0";
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            acc.Update(info);

            DirectoryInfo DirInfo = new DirectoryInfo(Server.MapPath(strPath));

            if (!DirInfo.Exists)
            {
                DirInfo.Create();
            }

            //if (FileUpload.UploadedFiles.Count > 0)
            //{
            //    UploadedFile file = FileUpload.UploadedFiles[0];
            //    serverFileName = Path.GetFileName(file.FileName);
            //    strFileUploadName = DateTime.Now.Ticks.ToString() + (serverFileName);
            //    strFileExtension = file.GetExtension();
            //    if (strFileExtension == ".exe")
            //    {
            //        main.MessageAlert(Page, "ไม่สามารถอัพโหลดไฟล์ที่มีนามสกุลเป็น .exe ได้");
            //        return;
            //    }
            //    else
            //    {
            //        file.SaveAs(Server.MapPath(strPath + strFileUploadName));
            //    }
            //}

            LoadRadGridBankAcc(null);
            RadGridBankAcc.DataBind();
            UpdateUI2("Update");
            ClearControl2();
        }

        protected void CheckDateValid(object source, ServerValidateEventArgs args)
        {
            if (!DateTimeX.IsValidDmyDate(args.Value, true) || args.Value == "")
            {
                args.IsValid = false;
            }
        }

        protected void ddlBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;

            if (strBgmSourceSeq.Trim().Length > 0)
            {
                //LoadDDLSubSourceSeq(strBgmSourceSeq);

                //if (txtBranchSeq.Text.Length > 0)
                //{
                //    RadTreeView tree = (RadTreeView)ddlBranchSeq.Items[0].FindControl("treeBranchSeq");
                //    //tree.FindNodeByValue(txtBranchSeq.Text).Selected = true;
                //    ddlBranchSeq.Text = tree.SelectedNode.Text;
                //}

                //LoadDDLSavingBankAccSeq(strBgmSourceSeq);
            }
            else
            {
                //LoadDDLSavingBankAccSeq("");
            }

            //PanelSubSource.Visible = ddlSubSourceSeq.Items.Count > 0;
        }

        protected void RadGridBankAcc_PageIndexChanged(object source, GridPageChangedEventArgs e)
        {
            LoadRadGridBankAcc(null);
        }

        protected void cboRadGrpName_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strBankAccTypeGrpSeq = cboRadGrpName.SelectedValue;

            if (strBankAccTypeGrpSeq == "2")
            {
                if (ddlBgmSourceSeq.Items.Count > 0)
                {
                    if (!(ddlBgmSourceSeq.SelectedIndex >= 0))
                        ddlBgmSourceSeq.SelectedIndex = 0;
                }
                pnSavingBankAccSeq.Visible = true;
            }
            else
            {
                pnSavingBankAccSeq.Visible = false;
            }
        }

        protected void ddlBank_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlBank.SelectedValue))
            {
                LoadBranch();
            }
            else
            {
                ddlBranch.DataSource = null;
                ddlBranch.DataBind();
            }
        }

       
        //==========================================================================
    }
}
